#!/bin/bash

# Build script for YOLOv8 OpenVINO C++ inference on Linux

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${GREEN}=== YOLOv8 OpenVINO C++ Build Script ===${NC}"

# Check if cmake is installed
if ! command -v cmake &> /dev/null; then
    echo -e "${RED}Error: cmake is not installed${NC}"
    exit 1
fi

# Check if g++ is installed
if ! command -v g++ &> /dev/null; then
    echo -e "${RED}Error: g++ is not installed${NC}"
    exit 1
fi

# Get script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BUILD_DIR="${SCRIPT_DIR}/build"

# Create build directory
echo -e "${YELLOW}Creating build directory...${NC}"
mkdir -p "${BUILD_DIR}"
cd "${BUILD_DIR}"

# Configure CMake
echo -e "${YELLOW}Configuring CMake...${NC}"
cmake .. \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_CXX_COMPILER=g++ \
    -DCMAKE_C_COMPILER=gcc

# Build
echo -e "${YELLOW}Building...${NC}"
make -j$(nproc)

# Create bin directory and copy executables
echo -e "${YELLOW}Copying executables...${NC}"
mkdir -p "${SCRIPT_DIR}/bin"
cp detect "${SCRIPT_DIR}/bin/"
cp detect_service "${SCRIPT_DIR}/bin/"

echo -e "${GREEN}Build completed successfully!${NC}"
echo -e "${GREEN}Executables:${NC}"
echo -e "  - ${SCRIPT_DIR}/bin/detect (visualization)"
echo -e "  - ${SCRIPT_DIR}/bin/detect_service (Go service)"

